<template>
  <div class="app-container">
    <el-card class="card-form">
      <div class="organization-title">机构管理</div>
      <el-form :model="queryForm" ref="queryForm" :inline="true">
        <el-form-item label="机构名称" prop="organizationName">
          <el-input
            v-model="queryForm.organizationName"
            placeholder="请输入"
            clearable
            size="small"
            style="width: 252px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="机构类型" prop="organizationType">
          <el-select
            v-model="queryForm.organizationType"
            placeholder="请选择"
            clearable
            size="small"
            style="width: 252px"
          >
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="添加日期">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 252px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="handleQuery"
            >查询</el-button
          >
          <el-button size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="card-table">
      <el-row :gutter="10" class="mb20 mt5">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="handleAdd"
            v-hasPermi="['tripartite:organization:add']"
            >添加机构</el-button
          >
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="organizationList" border>
        <el-table-column
          label="序号"
          align="center"
          width="65"
          type="index"
        />
        <el-table-column
          label="机构名称"
          align="center"
          prop="organizationName"
          :show-overflow-tooltip="true"
          width="260"
        />
        <el-table-column
          label="机构类型"
          align="center"
          prop="organizationType"
          :show-overflow-tooltip="true"
          width="255"
        />
        <el-table-column
          label="联系人"
          align="center"
          prop="contacts"
          width="255"
        />
        <el-table-column
          label="联系电话"
          align="center"
          prop="contactNumber"
          width="255"
        />
        <el-table-column
          label="添加时间"
          align="center"
          prop="createTime"
          width="255"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleView(scope.row)"
              >查看</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleEdit(scope.row)"
              v-hasPermi="['tripartite:organization:edit']"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['tripartite:organization:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryForm.pageNum"
        :limit.sync="queryForm.pageSize"
        @pagination="getList"
      /> -->
      <el-pagination
        v-show="total > 0"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="queryForm.pageSize"
        :current-page="queryForm.pageNum"
        :page-sizes="[10, 20, 30, 50, 100]"
      >
      </el-pagination>
    </el-card>

    <!-- 添加或修改机构对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="628px"
      append-to-body
      custom-class="dialog-box"
    >
      <el-form
        ref="formData"
        :model="formData"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="机构名称" prop="organizationName">
          <el-input
            v-model="formData.organizationName"
            placeholder="请输入"
            style="width: 400px"
            maxlength="20"
          />
        </el-form-item>
        <el-form-item label="机构类型" prop="organizationType">
          <el-select
            v-model="formData.organizationType"
            placeholder="请选择"
            clearable
            style="width: 400px"
          >
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input
            v-model="formData.contacts"
            placeholder="请输入"
            style="width: 400px"
            maxlength="10"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactNumber">
          <el-input
            v-model="formData.contactNumber"
            placeholder="请输入"
            style="width: 400px"
            maxlength="13"
          />
        </el-form-item>
        <el-form-item label="传真" prop="fax">
          <el-input
            v-model="formData.fax"
            placeholder="请输入"
            style="width: 400px"
            maxlength="13"
          />
        </el-form-item>
        <el-form-item label="联系地址" prop="address">
          <el-input
            v-model="formData.address"
            placeholder="请输入"
            style="width: 400px"
            maxlength="30"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 查看机构对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="openDataScope"
      width="700px"
      append-to-body
      custom-class="dialog-box"
    >
      <el-descriptions class="margin-top" :column="1" border>
        <el-descriptions-item>
          <template slot="label"> 机构名称 </template>
          {{ rowData.organizationName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 机构类型 </template>
          {{ rowData.organizationType }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 联系人 </template>
          {{ rowData.contacts }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 联系电话 </template>
          {{ rowData.contactNumber }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 传真 </template>
          {{ rowData.fax }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 联系地址 </template>
          {{ rowData.address }}
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDataScope">取 消</el-button>
        <el-button type="primary" @click="cancelDataScope">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRole,
  getRole,
  delRole,
  addRole,
  updateRole,
  exportRole,
  dataScope,
  changeRoleStatus,
} from "@/api/system/role";
// import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
// import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept";
import {
  listManagement,
  getManagement,
  addManagement,
  updateManagement,
  delManagement,
} from "@/api/tripartite/organization";

export default {
  name: "Organization",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 机构管理表格数据
      organizationList: [],
      // 当前行表格数据
      rowData: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（查看）
      openDataScope: false,
      // 日期范围
      dateRange: [],
      // 机构类型数据字典
      typeOptions: [
        { dictValue: "审批服务机构", dictLabel: "审批服务机构" },
        { dictValue: "监理机构", dictLabel: "监理机构" },
        { dictValue: "设计单位", dictLabel: "设计单位" },
        { dictValue: "开发单位", dictLabel: "开发单位" },
        { dictValue: "安全检测单位", dictLabel: "安全检测单位" },
      ],
      // 查询参数
      queryForm: {
        pageNum: 1,
        pageSize: 10,
        organizationName: undefined,
        createTime: undefined,
        organizationType: undefined,
      },
      // 表单参数
      formData: {},
      // 表单校验
      rules: {
        organizationName: [
          { required: true, message: "机构名称不能为空", trigger: "blur" },
        ],
        organizationType: [
          { required: true, message: "请选择机构类型", trigger: "change" },
        ],
        contacts: [
          { required: true, message: "联系人不能为空", trigger: "blur" },
        ],
        contactNumber: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
        ],
        fax: [{ required: true, message: "传真不能为空", trigger: "blur" }],
        address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then((response) => {
      this.statusOptions = response.data;
      // console.log(this.statusOptions);
    });
  },
  methods: {
    /** 查询机构管理列表 */
    getList() {
      this.loading = true;
      listManagement(this.addDateRange(this.queryForm, this.dateRange)).then(
        (response) => {
          this.organizationList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 表单重置
    reset() {
      this.formData = {
        address: "",
        contactNumber: "",
        contacts: "",
        fax: "",
        organizationName: "",
        organizationType: "",
      };
      this.resetForm("formData");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机构";
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.reset();
      const ormId = row.ormId
      getManagement(ormId).then((response) => {
        this.formData = response.data;
        this.open = true;
        this.title = "编辑机构";
      });
    },
    /** 查看按钮操作*/
    handleView(row) {
      this.reset();
      const ormId = row.ormId;
      getManagement(ormId).then((response) => {
        this.rowData = response.data;
        this.openDataScope = true;
        this.title = "查看机构";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          if (this.formData.ormId == undefined || this.formData.ormId == "" || !this.formData.ormId) {
            addManagement(this.formData).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } else {
            updateManagement(this.formData).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ormId = row.ormId 
      this.$confirm("是否确认删除 ？", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "del_box",
      })
        .then(function () {
          return delManagement(ormId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 分页 */
    handleSizeChange(val) {
      this.queryForm.pageSize=val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum=val
      this.getList()
    }
  },
};
</script>
<style scoped lang="scss">
.organization-title {
  font-size: 18px;
  font-family: PingFang SC, PingFang SC-Medium;
  font-weight: 500;
  margin-bottom: 24px;
  color: #000000;
}
.card-table {
  margin-top: 10px;
  .el-pagination {
    text-align: center;
    margin-top: 40px;
  }
}
</style>
<style lang="scss">
/**表单label取消加粗 */
.card-form {
  .el-card__body {
    padding-bottom: 0;
  }
  label {
    font-weight: normal !important;
  }
  .el-form--inline .el-form-item {
    margin-right: 60px;
  }
}
/* 弹出窗样式 */
.dialog-box {
  .el-dialog__body {
    padding: 24px 69px 24px 69px;
    border-top: 1px solid #ebecef;
    border-bottom: 1px solid #ebecef;
    label {
      font-weight: 400 !important;
    }
  }
  .el-dialog__footer {
    padding: 16px 20px;
  }
  .el-dialog__headerbtn {
    background: url("../../../assets/images/icon_dialog_close.png");
    background-size: 100%;
    width: 16px;
    height: 16px;
    .el-dialog__close {
      visibility: hidden;
    }
  }
}
/* el-descriptions-item 文字居中 */
.el-descriptions__body .el-descriptions__table .el-descriptions-item__cell {
  text-align: center;
}
/* 删除弹出框样式 */
.del_box {
  width: 345px;
  height: 175px;
  .el-message-box__headerbtn {
    background: url("../../../assets/images/icon_dialog_close.png");
    background-size: 100%;
    width: 16px;
    height: 16px;
    .el-message-box__close {
      visibility: hidden;
    }
  }
  .el-message-box__content {
    padding: 26px 95px;
    font-size: 16px;
    .el-message-box__status.el-icon-warning {
      color: #ea4747;
    }
    .el-message-box__status + .el-message-box__message {
      padding-right: 0;
    }
  }
}
</style>
